// https://leetcode.cn/problems/symmetric-tree/?envType=study-plan-v2&envId=top-interview-150

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool check(TreeNode* leftTree, TreeNode* rightTree) {
        if (!leftTree && !rightTree) {
            return true;
        } else if (!leftTree || !rightTree) {
            return false;
        } else if (leftTree->val != rightTree->val) {
            return false;
        } else {
            return check(leftTree->left, rightTree->right) && check(leftTree->right, rightTree->left);
        }
    }

    bool isSymmetric(TreeNode* root) {
        if (!root) {
            return true;
        }

        return check(root->left, root->right);
    }
};